package nix.demo3.web.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nix.commons.CommonUtils;
import nix.demo3.domain.Customer;
import nix.demo3.domain.PageBean;
import nix.demo3.service.CustomerService;
import nix.servlet.BaseServlet;

public class CustomerServlet extends BaseServlet {

	private CustomerService customerService = new CustomerService();

	public String add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		Customer customer = CommonUtils.toBean(request.getParameterMap(),
				Customer.class);
		customer.setCid(CommonUtils.uuid());

		customerService.add(customer);
		request.setAttribute("msg", "恭喜，添加客户成功！");
		return "f:/demo3/msg.jsp";
	}

	public String findAll(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		int pc = getPc(request);
		int ps = 10;
		PageBean<Customer> pb = customerService.findAll(pc, ps);
		pb.setUrl(getUrl(request));
		
		request.setAttribute("pb", pb);
		return "f:/demo3/list.jsp";
	}
	
	private int getPc(HttpServletRequest request) {
		String value = request.getParameter("pc");
		if (value == null || value.trim().isEmpty()) {
			return 1;
		} else {
			return Integer.parseInt(value);
		}
	}

	public String preEdit(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String cid = request.getParameter("cid");
		request.setAttribute("cstm", customerService.load(cid));
		return "f:/demo3/edit.jsp";
	}

	public String edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		Customer customer = CommonUtils.toBean(request.getParameterMap(),
				Customer.class);

		customerService.edit(customer);
		request.setAttribute("msg", "恭喜，编辑客户成功！");
		return "f:/demo3/msg.jsp";
	}

	public String delete(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String cid = request.getParameter("cid");
		customerService.delete(cid);
		request.setAttribute("msg", "恭喜，删除客户成功！");
		return "f:/demo3/msg.jsp";
	}

	public String query(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int pc = getPc(request);
		int ps = 10;
		Customer criteria = CommonUtils.toBean(request.getParameterMap(),
				Customer.class);
		criteria = encoding(criteria);
		
		PageBean<Customer> pb = customerService.query(criteria, pc, ps);
		pb.setUrl(getUrl(request));
		
		request.setAttribute("pb", pb);
		return "f:/demo3/list.jsp";
	}
	
	private Customer encoding(Customer criteria) throws UnsupportedEncodingException {
		String cname = criteria.getCname();
		String gender = criteria.getGender();
		String cellphone = criteria.getCellphone();
		String email = criteria.getEmail();
		
		if (cname != null && !cname.trim().isEmpty()) {
			cname = new String(cname.getBytes("ISO-8859-1"), "utf-8");
			criteria.setCname(cname);
		}
		if (gender != null && !gender.trim().isEmpty()) {
			gender = new String(gender.getBytes("ISO-8859-1"), "utf-8");
			criteria.setGender(gender);
		}
		if (cellphone != null && !cellphone.trim().isEmpty()) {
			cellphone = new String(cellphone.getBytes("ISO-8859-1"), "utf-8");
			criteria.setCellphone(cellphone);
		}
		if (email != null && !email.trim().isEmpty()) {
			email = new String(email.getBytes("ISO-8859-1"), "utf-8");
			criteria.setEmail(email);
		}
		
		return criteria;
	}

	private String getUrl(HttpServletRequest request) {
		String contextPath = request.getContextPath();
		String servletPath = request.getServletPath();
		String queryString = request.getQueryString();
		
		if (queryString.contains("&pc=")) {
			int index = queryString.lastIndexOf("&pc=");
			queryString = queryString.substring(0, index);
		}
		
		return contextPath + servletPath + "?" +queryString;
	}
}
